{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "fcf4ba88-2b06-4539-9232-83ae9380216d",
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "from openai import OpenAI\n",
    "\n",
    "# 国内代理方式\n",
    "client = OpenAI(\n",
    "    api_key = \"sk-y7DHfp9fzuCxOVm2158638099f9541D3833aB4F4Ed674aCf\",\n",
    "    base_url = \"https://vip.apiyi.com/v1\"    # 此处代理方式，如果是OpenAI官方接口需调整接口地址\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "4cd3b8f2-f75b-4edc-a74c-6dc898ff77ae",
   "metadata": {},
   "outputs": [],
   "source": [
    "speech_file_path = \"./audio/liyunlong.mp3\"\n",
    "\n",
    "# 官方示例的用法会触发 Deprecated 警告⚠️，已替换为最佳实践\n",
    "with client.audio.speech.with_streaming_response.create(\n",
    "    model=\"tts-1\",\n",
    "    voice=\"echo\",\n",
    "    input=\"二营长！你他娘的意大利炮呢？给我拉来！\"\n",
    ") as response:\n",
    "    response.stream_to_file(speech_file_path)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "bb41817f-68d2-47a0-838a-627b85dd3c1e",
   "metadata": {},
   "outputs": [],
   "source": [
    "speech_file_path = \"./audio/quewang.mp3\"\n",
    "\n",
    "with client.audio.speech.with_streaming_response.create(\n",
    "    model=\"tts-1\",\n",
    "    voice=\"onyx\",\n",
    "    input=\"周三早上11点，雀王争霸赛，老地方23号房，经典三缺一！\"\n",
    ") as response:\n",
    "    response.stream_to_file(speech_file_path)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "052fca93-cf4b-4874-bc7f-c016e11b1b06",
   "metadata": {},
   "outputs": [],
   "source": [
    "speech_file_path = \"./audio/boyin.mp3\"\n",
    "\n",
    "with client.audio.speech.with_streaming_response.create(\n",
    "    model=\"tts-1\",\n",
    "    voice=\"onyx\",\n",
    "    input=\"\"\"\n",
    "    上海F1赛车时隔五年回归 首位中国车手周冠宇：我渴望站上领奖台\n",
    "    2024年4月17日\n",
    "    阔别五年的世界一级方程式（F1）赛车中国站即将于2024年4月19至21日在上海国际赛车场举行，并首次有中国籍赛车手参赛。\n",
    "    \n",
    "    作为中国第一位F1赛车手，24岁的上海小伙周冠宇称自己“渴望站上领奖台”。\n",
    "    \"\"\"\n",
    ") as response:\n",
    "    response.stream_to_file(speech_file_path)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6f106f37-ecc7-4cc8-973a-977922e6a6a6",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
